package com.qiaofang.applet.service.property.service.impl;

import com.qiaofang.applet.common.base.MongoBaseService;
import com.qiaofang.applet.common.context.QfContextHolder;
import com.qiaofang.applet.common.util.RedisKeyUtil;
import com.qiaofang.applet.common.util.RedisUtil;
import com.qiaofang.applet.service.property.domain.Area;
import com.qiaofang.applet.service.property.repository.AreaRepository;
import com.qiaofang.applet.service.property.service.AreaService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@Slf4j
@Service
public class AreaServiceImpl extends MongoBaseService<Area, String> implements AreaService {

    private final AreaRepository repository;

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;

    public AreaServiceImpl(AreaRepository repository) {
        super(repository);
        this.repository = repository;
    }

    @Override
    public Area findByAreaUuidAndCompanyUuid(String areaUuid, String companyUuid) {
        return repository.findByAreaUuidAndCompanyUuid(areaUuid, companyUuid);
    }

    @Override
    public List<Area> findAllByDistrictUuid(String districtUuid) {
        List<Area> areaList = repository.findAllByDistrictUuidAndCorporationCode(districtUuid, QfContextHolder.getContext().getCorporationCode());
        areaList = areaList.stream().collect(Collectors.collectingAndThen(
                Collectors.toCollection(() -> new TreeSet<>(
                        Comparator.comparing(
                                Area::getAreaUuid))), ArrayList::new));
        return areaList;
    }

    @Override
    public List<Area> findAllByCityCode(String cityCode) {
        return repository.findAllByCorporationCodeAndCityCode(QfContextHolder.getContext().getCorporationCode(), cityCode);
    }

    @Override
    public List<Area> findAllByCityCode(String corporationCode, String cityCode) {
        return repository.findAllByCorporationCodeAndCityCode(corporationCode, cityCode);
    }

    @Override
    public Map<String, String> getAllAreaMap(String corporationCode) {
        String areaMapKey = RedisKeyUtil.getAreaMapKey(corporationCode);
        Object obj = redisUtil.get(areaMapKey);
        if (obj != null) {
            return (Map<String, String>) obj;
        }
        Map<String, String> areaMap = new HashMap<>();
        List<Area> list = repository.findAllByCorporationCode(corporationCode);
        if (!CollectionUtils.isEmpty(list)){
            for (Area area : list) {
                areaMap.put(area.getAreaUuid(), area.getAreaName());
            }
            redisTemplate.opsForHash().putAll(areaMapKey, areaMap);
            redisTemplate.expire(areaMapKey, 86400 - DateUtils.getFragmentInSeconds(Calendar.getInstance(), Calendar.DATE), TimeUnit.SECONDS);
        }
        return areaMap;
    }


}
